package com.cup.bddm.impala.controller;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cup.bddm.handler.Token;
import com.cup.bddm.impala.Constant;
import com.cup.bddm.impala.service.BranchService;
import com.cup.bddm.impala.vo.BranchServiceMchntStatVo;
import com.cup.bddm.impala.vo.BranchStreetStatVo;
import com.cup.bddm.impala.vo.BranchTransDistVo;
import com.cup.bddm.impala.vo.BranchTransInfoVo;
import com.cup.bddm.impala.vo.CityGeneDailyTransVo;
import com.cup.bddm.util.DateUtil;

@Token
@Controller("srv-BranchController")
@RequestMapping(value = "/services/branch", method = RequestMethod.GET)
public class BranchController {

	static Logger logger = Logger.getLogger(BranchController.class);

	@Autowired
	private BranchService branchService;

	@RequestMapping(value = "/general/{branchCd}/{date}")
	@ResponseBody
	public CityGeneDailyTransVo getCityGeneDailyTransInfo(@PathVariable("branchCd") String branchCd, @PathVariable("date") String date) {
		logger.info("branch trans info trans request");
		CityGeneDailyTransVo result = branchService.getCityGeneDailyTransInfo(branchCd, date);
		return result;
	}

	@RequestMapping(value = "/trans/{branchCd}/{date}")
	@ResponseBody
	public BranchTransInfoVo getBranchTransInfo(@PathVariable("branchCd") String branchCd, @PathVariable("date") String date) {
		logger.info("branch trans info request. branchCd=" + branchCd + ",date=" + date);
		BranchTransInfoVo result = new BranchTransInfoVo();
		if (StringUtils.isEmpty(branchCd) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}
		result = branchService.getBranchTransInfo(branchCd, date);
		return result;
	}

	@RequestMapping(value = "/transTpDis/{branchCd}/{date}")
	@ResponseBody
	public BranchTransDistVo getBranchTransByTransType(@PathVariable("branchCd") String branchCd, @PathVariable("date") String date) {
		logger.info("branch trans type distribute info request. branchCd=" + branchCd + ",date=" + date);
		BranchTransDistVo result = new BranchTransDistVo();
		if (StringUtils.isEmpty(branchCd) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}
		result = branchService.getBranchTransByTransType(branchCd, date);
		return result;
	}

	@RequestMapping(value = "/mchntTpDis/{branchCd}/{date}")
	@ResponseBody
	public BranchTransDistVo getBranchTransByMchntType(@PathVariable("branchCd") String branchCd, @PathVariable("date") String date) {
		logger.info("branch mchnt type distribute info request. branchCd=" + branchCd + ",date=" + date);
		BranchTransDistVo result = new BranchTransDistVo();
		if (StringUtils.isEmpty(branchCd) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}
		result = branchService.getBranchTransByMchntType(branchCd, date);
		return result;
	}

	@RequestMapping(value = "/streetStat/{branchCd}/{date}")
	@ResponseBody
	public BranchStreetStatVo getBranchStreetStatInfo(@PathVariable("branchCd") String branchCd, @PathVariable("date") String date) {
		logger.info("branch street stat info request. branchCd=" + branchCd + ",date=" + date);
		BranchStreetStatVo result = new BranchStreetStatVo();
		if (StringUtils.isEmpty(branchCd) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}
		result = branchService.getBranchStreetStatInfo(branchCd, date);
		return result;
	}

	@RequestMapping(value = "/serviceMchntStat/{branchCd}/{date}")
	@ResponseBody
	public BranchServiceMchntStatVo getBranchServiceMchntStatInfo(@PathVariable("branchCd") String branchCd, @PathVariable("date") String date) {
		logger.info("branch service merchant stat info request. branchCd=" + branchCd + ",date=" + date);
		BranchServiceMchntStatVo result = new BranchServiceMchntStatVo();
		if (StringUtils.isEmpty(branchCd) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}
		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}
		result = branchService.getBranchServiceMchntStatInfo(branchCd, date);
		return result;
	}

}
